﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;

using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace Voluntariar.WEB.Util
{
    public class AnexoEmail
    {
        public static string senha = "ffelipinto";
        public string Caminho { get; set; }
        public string Nome { get; set; }
        public MemoryStream Conteudo { get; set; }

        public AnexoEmail()
        {
        }

        public AnexoEmail(string Caminho, string Nome)
        {
            this.Nome = Nome;
            this.Caminho = Caminho;
            Conteudo = null;
        }
        public AnexoEmail(string Nome, byte[] Conteudo)
        {
            this.Nome = Nome;
            this.Conteudo = new MemoryStream();
            this.Conteudo.Write(Conteudo, 0, Conteudo.Length);
            this.Conteudo.Position = 0;
        }

        public AnexoEmail Clonar()
        {
            AnexoEmail Novo = new AnexoEmail();
            Novo.Caminho = this.Caminho;
            Novo.Nome = this.Nome;
            Novo.Caminho = this.Caminho;
            if (Conteudo != null)
            {
                Novo.Conteudo = new MemoryStream();
                Conteudo.CopyTo(Novo.Conteudo);
                Novo.Conteudo.Position = 0;
                this.Conteudo.Position = 0;
            }
            return Novo;
        }
    }

    public class Email
    {

        public string Erro { get; set; }
        public List<string> ListaDestinatarios { get; set; }
        public List<AnexoEmail> ListaAnexos { get; set; }

        public Email()
        {

        }

        public bool Enviarici(string Assunto, string Mensagem, string destinatario, bool IsHtml = false)
        {
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();

                message.From = new MailAddress("luizfsantos@ici.curitiba.org.br");
                message.To.Add(new MailAddress(destinatario));
                message.Subject = Assunto;
                message.Body = Mensagem;

                if (IsHtml)
                    message.IsBodyHtml = true;

                smtp.Port = 25;
                smtp.Host = "smtp.ici.curitiba.org.br";
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("luizfsantos@ici.curitiba.org.br", AnexoEmail.senha);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                smtp.Send(message);

                return true;
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                    this.Erro = e.Message;
                else
                    this.Erro = e.InnerException.Message;
                return false;

            }
        }

        public bool Enviaryahoo(string Assunto, string Mensagem, string destinatario, bool IsHtml = false)
        {
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();

                message.From = new MailAddress("luizfsantos@ici.curitiba.org.br");
                message.To.Add(new MailAddress(destinatario));
                message.Subject = Assunto;
                message.Body = Mensagem;

                if (IsHtml)
                    message.IsBodyHtml = true;

                smtp.Port = 465;
                smtp.Host = "smtp.mail.yahoo.org.br";
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("luizfsantos@ici.curitiba.org.br", AnexoEmail.senha);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                smtp.Send(message);

                return true;
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                    this.Erro = e.Message;
                else
                    this.Erro = e.InnerException.Message;
                return false;

            }
        }

        public bool EnviarGmail(string Assunto, string Mensagem, string destinatario, bool IsHtml = false)
        {
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();

                message.From = new MailAddress("ffelipeluiz25@gmail.com");
                message.To.Add(new MailAddress(destinatario));
                message.Subject = Assunto;
                message.Body = Mensagem;

                smtp.Port = 587;
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("ffelipeluiz25@gmail.com", AnexoEmail.senha);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                smtp.Send(message);

                return true;
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                    this.Erro = e.Message;
                else
                    this.Erro = e.InnerException.Message;
                return false;

            }
        }

    }
}